/**
 * Copyright 2016 Hani Al Momani
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vanniktech.emoji.Actions;

import com.vanniktech.emoji.EmojiconRecentsManager;
import com.vanniktech.emoji.EmojiconTextField;
import com.vanniktech.emoji.EmojiconsPopup;
import com.vanniktech.emoji.ResourceTable;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.StackLayout;
import ohos.app.Context;

/**
 * EmojIconActions
 *
 * @author Hani Al Momani (hani.momanii@gmail.com)
 */
public class EmojIconActions implements Component.FocusChangedListener {
    private EmojiconsPopup popup;
    private int showPixmap;
    private int hidePixmap;
    private Image emojiButton;
    private EmojiconTextField emojiconTextField;
    private EmojiconTextField[] emojiconTextFieldList = new EmojiconTextField[2];
    private int keyBoardIcon = ResourceTable.Media_ic_action_keyboard;
    private int smileyIcons = ResourceTable.Media_smiley;

    /**
     * Constructor
     *
     * @param ctx        The context of current ability.
     * @param rootView   The top most layout in your view hierarchy. The difference of this view and the screen height will be used to calculate the keyboard height.
     * @param width      宽
     * @param height     高
     * @param recentsManager     EmojiconRecentsManager
     * @param textFields 输入框
     */
    public EmojIconActions(Context ctx, StackLayout rootView,
                           int width, int height, EmojiconRecentsManager recentsManager, EmojiconTextField... textFields) {
        this.popup = new EmojiconsPopup(ctx, width, height, recentsManager, textFields);
        rootView.addComponent(popup);
        setCursorVisible(textFields);
        initListeners();
    }

    /**
     * Constructor
     *
     * @param ctx         The context of current ability.
     * @param rootView    The top most layout in your view hierarchy. The difference of this view and the screen height will be used to calculate the keyboard height.
     * @param width       宽
     * @param height      高
     * @param emojiButton The Id of ImageButton used to open Emoji
     * @param recentsManager EmojiconRecentsManager
     * @param textFields  输入框
     */
    public EmojIconActions(Context ctx, StackLayout rootView, int width, int height, Image emojiButton, EmojiconRecentsManager recentsManager, EmojiconTextField... textFields) {
        this.emojiButton = emojiButton;
        this.popup = new EmojiconsPopup(ctx, width, height, recentsManager, textFields);
        this.emojiconTextFieldList = textFields;
        rootView.addComponent(popup);
        setCursorVisible(textFields);
        initListeners();
    }

    private void setCursorVisible(EmojiconTextField[] textFields) {
        for (EmojiconTextField textField : textFields) {
            if ("1".equals(textField.getTag())) {
                textField.setFocusable(Component.FOCUS_ENABLE);
                textField.setEnabled(true);
                textField.requestFocus();
                textField.setTextCursorVisible(true);
            } else {
                textField.clearFocus();
            }
        }
    }

    /**
     * getPopup
     *
     * @return EmojiconsPopup
     */
    public EmojiconsPopup getPopup() {
        return popup;
    }

    /**
     * setEmojiButton
     *
     * @param emojiButton The Id of ImageButton used to open Emoji
     */
    public void setEmojiButton(Image emojiButton) {
        this.emojiButton = emojiButton;
        initEmojiButtonListener();
    }

    /**
     * 设图片
     *
     * @param showpixmap 显示
     * @param hidepixmap 隐藏
     * @noinspection checkstyle:JavadocMethod
     */
    public void setEmojiButtonPixmap(int showpixmap, int hidepixmap) {
        this.showPixmap = showpixmap;
        this.hidePixmap = hidepixmap;
    }

    /**
     * 初始化监听
     */
    public void initListeners() {
        initEmojiButtonListener();
    }

    private void initEmojiButtonListener() {
        if (emojiButton != null) {
            emojiButton.setClickedListener(v -> togglePopupVisibility());
        }
    }

    private void togglePopupVisibility() {
        if (popup.getVisibility() == Component.HIDE) {
            showPopup();
        } else {
            hidePopup();
        }
        setCursorVisible(emojiconTextFieldList);
    }

    /**
     * 显示
     */
    public void showPopup() {
        if (popup != null && popup.getVisibility() == Component.HIDE) {
            changeEmojiKeyboardIcon(emojiButton, showPixmap);
            popup.show();
        }
        visiblePopup();
    }

    /**
     * 显示
     */
    public void showPopupFromFloat() {
        if (popup != null) {
            changeEmojiKeyboardIcon(emojiButton, showPixmap);
            popup.showFloat();
        }
        visiblePopup();
    }

    /**
     * 隐藏
     */
    public void hidePopup() {
        if (popup != null && popup.getVisibility() == Component.VISIBLE) {
            changeEmojiKeyboardIcon(emojiButton, hidePixmap);
            popup.hide();
        }
        verticalPopup();
    }

    /**
     * 不显示
     */
    public void invisiblePopup() {
        if (popup != null && popup.getVisibility() == Component.VISIBLE) {
            changeEmojiKeyboardIcon(emojiButton, hidePixmap);
            popup.setVisibility(Component.INVISIBLE);
        }
        verticalPopup();
    }

    /**
     * 隐藏
     */
    public void verticalPopup() {
        if (popup != null && popup.getVisibility() == Component.VISIBLE) {
            changeEmojiKeyboardIcon(emojiButton, hidePixmap);
            popup.setVisibility(Component.VERTICAL);
        }
    }

    /**
     * 显示
     */
    public void visiblePopup() {
        if (popup != null && (popup.getVisibility() == Component.VISIBLE || popup.getVisibility() == Component.INVISIBLE)) {
            changeEmojiKeyboardIcon(emojiButton, hidePixmap);
            popup.setVisibility(Component.VISIBLE);
        }
    }

    private void changeEmojiKeyboardIcon(Image iconToBeChanged, int drawableResourceId) {
        if (iconToBeChanged != null) {
            iconToBeChanged.setPixelMap(drawableResourceId);
        }
    }

    /**
     * getVisibility
     *
     * @return int 状态
     */
    public int getVisibility() {
        return popup.getVisibility();
    }

    /**
     * 设置颜色
     *
     * @param tabsColor       导航
     * @param backgroundColor 背景色
     */
    public void setColors(int tabsColor, int backgroundColor) {
        this.popup.setColors(tabsColor, backgroundColor);
    }

    /**
     * 设置id
     *
     * @param keyboardIcon 键盘
     * @param smileyIcon   笑脸
     */
    public void setIconsIds(int keyboardIcon, int smileyIcon) {
        this.keyBoardIcon = keyboardIcon;
        this.smileyIcons = smileyIcon;
    }

    /**
     * 添加
     *
     * @param editTexts 输入框
     */
    public void addEmojiconEditTextList(EmojiconTextField... editTexts) {
        emojiconTextFieldList = editTexts;
        for (EmojiconTextField editText : editTexts) {
            editText.setFocusChangedListener(this);
        }
    }

    /**
     * 焦点改变
     *
     * @param component component
     * @param isFocus   isFocus
     */
    @Override
    public void onFocusChange(Component component, boolean isFocus) {
        if (isFocus) {
            if (component instanceof EmojiconTextField) {
                emojiconTextField = (EmojiconTextField) component;
            }
        }
    }

    /**
     * 使用系统
     *
     * @param isSystemEmoji 标记
     */
    public void setUseSystemEmoji(boolean isSystemEmoji) {
        for (EmojiconTextField editText : emojiconTextFieldList) {
            editText.setUseSystemDefault(isSystemEmoji);
        }
    }
}
